//
//	UDP Client
//

#include <stdio.h>
#include <conio.h>
#include <exception>
#include "winsock2.h"

using namespace std;

#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "Advapi32.lib")

typedef unsigned int uint;
#define LOGF			printf
#define LOG_VALUE(val)	log_value(#val, val)
#define LOG_INIT(s)		printf("INIT : %s\r\n", s)
#define LOG_SHUTDOWN(s)	printf("SHUTDOWN : %s\r\n", s)

inline void log_value(const char *name, int   val)			{ LOGF("%-30s = %d\r\n", name, val); }
inline void log_value(const char *name, float val)			{ LOGF("%-30s = %f\r\n", name, val); }
inline void log_value(const char *name, const char *val)	{ LOGF("%-30s = %s\r\n", name, val); }


#include "..\udp_sv\net_helper.h"
#include "..\udp_sv\net_shared.h"


class EClient {
	public:
					EClient		( uint port );
					~EClient	( void );
		void		Frame		( void );
	protected:
		uint		server_port;
		sockaddr_in	addr_broadcast;
		SOCKET		cl_socket;
	};


EClient::EClient(uint port) 
{
	server_port	=	port;
	int r = 0;
	unsigned long enable = 1;
	int one = 1;
	

	WSADATA		wsaData;
	WSAStartup(MAKEWORD(2,2), &wsaData);
	
	cl_socket	=	socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

	addr_broadcast.sin_family		=	AF_INET;
	addr_broadcast.sin_port			=	htons(server_port);
	addr_broadcast.sin_addr.s_addr	=	inet_addr("255.255.255.255");

	//	TODO : make function for socket creation :
	CHECK_WSA( ioctlsocket( cl_socket, FIONBIO, &enable ) );
	CHECK_WSA( setsockopt( cl_socket, SOL_SOCKET, SO_BROADCAST, (char*)&one, sizeof(one)) );
	
	ENetPacket_s	packet;
	sprintf(packet.data, "blah-blah-blah");
	
	CHECK_WSA( sendto(cl_socket, (char*)&packet, MAX_PACKET_SIZE, 0, (sockaddr*)&addr_broadcast, sizeof(addr_broadcast)) );
}


void EClient::Frame( void )
{
	
}


EClient::~EClient( void )
{
}



void main() {

	try {
		EClient	cl(12345);
		
		while (1) {
			Sleep(5);
			cl.Frame();
		}
	} catch (exception &e) {
		LOGF("ERROR : %s\r\n", e.what());
		getch();
	}


  /*WSADATA wsaData;
  SOCKET RecvSocket;
  sockaddr_in RecvAddr;
  int Port = 27015;
  char RecvBuf[1024];
  int  BufLen = 1024;
  sockaddr_in SenderAddr;
  int SenderAddrSize = sizeof(SenderAddr);

  //-----------------------------------------------
  // Initialize Winsock
  WSAStartup(MAKEWORD(2,2), &wsaData);

  //-----------------------------------------------
  // Create a receiver socket to receive datagrams
  RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

  //-----------------------------------------------
  // Bind the socket to any address and the specified port.
  RecvAddr.sin_family = AF_INET;
  RecvAddr.sin_port = htons(Port);
  RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);

  bind(RecvSocket, (SOCKADDR *) &RecvAddr, sizeof(RecvAddr));

  //-----------------------------------------------
  // Call the recvfrom function to receive datagrams
  // on the bound socket.
  printf("Receiving datagrams...\n");
  recvfrom(RecvSocket, 
    RecvBuf, 
    BufLen, 
    0, 
    (SOCKADDR *)&SenderAddr, 
    &SenderAddrSize);

  //-----------------------------------------------
  // Close the socket when finished receiving datagrams
  printf("Finished receiving. Closing socket.\n");
  closesocket(RecvSocket);

  //-----------------------------------------------
  // Clean up and exit.
  printf("Exiting.\n");
  WSACleanup();
  return;	*/
}
